package rdd.operate;

import org.apache.spark.SparkConf;
import org.apache.spark.api.java.JavaPairRDD;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.JavaSparkContext;
import scala.Tuple2;

import java.util.ArrayList;
import java.util.List;

//User不能直接比较排序，需要实现comparable接口，重写comparaTo方法
public class Spark59_Operate_sortByKey_1 {
    public static void main(String[] args) {
        final SparkConf conf = new SparkConf();
        conf.setMaster("local");
        conf.setAppName("spark");
        final JavaSparkContext jsc = new JavaSparkContext(conf);

        final List<Tuple2<String,Integer>> datas = new ArrayList<>();
        datas.add(new Tuple2<String,Integer>("a",1));
        datas.add(new Tuple2<String,Integer>("b",3));
        datas.add(new Tuple2<String,Integer>("a",4));
        datas.add(new Tuple2<String,Integer>("b",2));

        final JavaRDD<Tuple2<String,Integer>> rdd = jsc.parallelize(datas);
        final JavaPairRDD<String,Integer> pairRDD = rdd.mapToPair(t -> t);
        pairRDD.sortByKey().collect().forEach(System.out::println);
    }
}
